package com.typhoon.spring_shiro.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.typhoon.spring_shiro.constants.ShiroConstants;
import com.typhoon.spring_shiro.constants.ShiroConstants.PermissionTypeEnum;
import com.typhoon.spring_shiro.dao.UserDao;
import com.typhoon.spring_shiro.dto.PermissionAppDto;
import com.typhoon.spring_shiro.dto.PermissionDto;
import com.typhoon.spring_shiro.dto.RoleDto;
import com.typhoon.spring_shiro.dto.UserDto;
import com.typhoon.spring_shiro.entity.User;
import com.typhoon.spring_shiro.exception.ServiceRuntimeException;
import com.typhoon.spring_shiro.service.PermissionAppService;
import com.typhoon.spring_shiro.service.PermissionService;
import com.typhoon.spring_shiro.service.RoleService;
import com.typhoon.spring_shiro.service.UserService;
import com.typhoon.spring_shiro.utils.BeanUtils;
import com.typhoon.spring_shiro.utils.CollectionUtils;

@Service("userService")
public class UserServiceImpl implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Resource
	private UserDao userDao;
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private PermissionService permissionService;
	
	@Resource
	 PermissionAppService permissionAppService;
	
	@Override
	public int save(UserDto paramE) {
		if(null == paramE)
			return -1;
		User user = BeanUtils.toBean(paramE, User.class);
		user = this.userDao.save(user);
		return null == user || null == user.getId() ? -1 : 1;
	}

	@Override
	public int saveBatch(List<? extends UserDto> paramList) {
		throw new ServiceRuntimeException("此方法暂未实现");
	}

	@Override
	public int update(UserDto paramE) {
		if(null == paramE)
			return -1;
		return this.userDao.update(BeanUtils.toBean(paramE, User.class));
	}

	@Override
	public int updateBatch(List<UserDto> paramList) {
		throw new ServiceRuntimeException("此方法暂未实现");
	}

	@Override
	public int deleteByPK(Long paramK) {
		if(null == paramK)
			return -1;
		return this.userDao.deleteByPrimaryKey(paramK);
	}

	@Override
	public int deleteBatchByPK(List<Long> paramList) {
		throw new ServiceRuntimeException("此方法暂未实现");
	}

	@Override
	public int delete(UserDto paramE) {
		throw new ServiceRuntimeException("此方法暂未实现");
	}

	@Override
	public int deleteBatch(UserDto paramE) {
		throw new ServiceRuntimeException("此方法暂未实现");
	}

	@Override
	public UserDto get(Object paramObject) {
		if(null == paramObject) 
			return null;
		return BeanUtils.toBean(this.userDao.get(paramObject),UserDto.class);
	}

	@Override
	public List<UserDto> list(Map<String, Object> paramMap) {
		if(null == paramMap)
			return null;
		return CollectionUtils.transform(this.userDao.list(paramMap), UserDto.class);
	}

	@Override
	public List<UserDto> list(Object paramObject) {
		if(null == paramObject)
			return null;
		return CollectionUtils.transform(this.userDao.list(paramObject), UserDto.class);
	}

	@Override
	public long count(Map<String, Object> paramMap) {
		if(null == paramMap || paramMap.isEmpty())
			return -1;
		return this.userDao.count(paramMap);
	}

	@Override
	public long count(Object paramObject) {
		if(null == paramObject)
			return -1;
		return this.userDao.count(paramObject);
	}

	@Override
	@Cacheable(value = "user",key="'id_'+#id")
	public UserDto queryByPK(Long paramK) {
		LOGGER.info("查询用户信息-{}",paramK);
		if(null == paramK)
			return null;
		User user = this.userDao.queryByPrimaryKey(paramK);
		return BeanUtils.toBean(user,UserDto.class);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Collection> getAuthorizationInfo(Long userId) {
		if(null == userId)
			return null;
		Map<String, Collection> authorizationMap = new HashMap<String, Collection>();
		List<RoleDto> roles =	roleService.listAllByUserId(userId);
		Set<String> roleSet = new HashSet<>();
		RoleDto role = null;
		for(int i = 0,size = roles.size(); i < size ; i ++) {
			role = roles.get(i);
			roleSet.add(role.getName());
		}
		List<PermissionDto> permissions = permissionService.listAllPermission(roles);
		Set<String> permissionSet = new HashSet<>();
		List<PermissionDto> appPermissions = new ArrayList<>();
		PermissionDto permission = null;
		for(int i = 0,size = permissions.size();i < size;i ++) {
			permission = permissions.get(i);
			permissionSet.add(permission.getCode());
			if(PermissionTypeEnum.APP.getKey().equals(permission.getType())) {//系统应用权限
				appPermissions.add(permission);
			}
		}
		
		List<PermissionAppDto> apps = permissionAppService.listAllByPermissions(appPermissions);
		LOGGER.info("当前登录用户:{}的角色集{}",userId,roleSet);
		LOGGER.info("当前登录用户:{}的权限集{}",userId,permissionSet);
		authorizationMap.put(ShiroConstants.APPS, apps);
		authorizationMap.put(ShiroConstants.ROLES, roleSet);
		authorizationMap.put(ShiroConstants.PERMISSIONS, permissionSet);
		
//		PermissionDto permission = null;
//		List<PermissionDto> appPermissions = new ArrayList<>();
//		List<PermissionDto> menuPermissions = new ArrayList<>();
//		List<PermissionDto> actionPermissions = new ArrayList<>();
//		for(int i = 0,size = permissions.size();i < size;i ++) {
//			permission = permissions.get(i);
//			if(PermissionTypeEnum.APP.getKey().equals(permission.getType())) {//系统应用权限
//				appPermissions.add(permission);
//			} else if(PermissionTypeEnum.MENU.getKey().equals(permission.getType())) {//菜单权限
//				menuPermissions.add(permission);
//			} else if(PermissionTypeEnum.ACTION.getKey().equals(permission.getType())) {//操作权限
//				actionPermissions.add(permission);
//			}
//		}
		
		return authorizationMap;
	}

}
